'use strict';

let fs = require('fs');
let path = require('path');
let router = require('koa-router')();

function seachFile(){
    return fs.readdirSync(__dirname).filter(name=>{
        return name.endsWith('.js') && name !== 'index.js';
    })
}

function resultFile(file){
    file.forEach(item=>{
        let filepath = path.join(__dirname,item);

        let obj = require(filepath);

        for(let i in obj){
            let type = obj[i][0];
            let fn = obj[i][1];
            if(type==='get'){
                router.get(i,fn);
            }else if(type==='post'){
                router.post(i,fn);
            }
        }
    })
}

module.exports=function(){
    let file = seachFile();
    resultFile(file);
    return router.routes();
}